package hotciv.view.tool;
import hotciv.framework.*;
import hotciv.view.*;
import minidraw.framework.*;
import minidraw.standard.*;
import java.awt.event.*;

public class MoveTool extends NullTool
{
	private DrawingEditor editor;
	private Game game;

	private Unit unitFrom;
	private Position from;
	
	public MoveTool(DrawingEditor editor, Game game) 
	{
		this.editor = editor;
		this.game = game;
	}
	
	public void mouseDown(MouseEvent e, int x, int y) 
	{
		from = GfxConstants.getPositionFromXY(x, y);
		unitFrom = game.getUnitAt(from);
		
		if (unitFrom != null) 
		{
			if (unitFrom.getOwner() != game.getPlayerInTurn()) 
				editor.showStatus("Not your " + unitFrom.getTypeString());
			else 
				editor.showStatus("Moving " + unitFrom.getTypeString());
		} 
	}
	
	
	public void mouseDrag(MouseEvent e, int x, int y)
	{
		Position to = GfxConstants.getPositionFromXY(x, y);
		Unit unitTo;
		Tile tile;
		
		if (to.getColumn() >= 0 && to.getColumn() < GameConstants.WORLDSIZE
		 && to.getRow() >= 0 && to.getRow() < GameConstants.WORLDSIZE) 
		{
			unitTo = game.getUnitAt(to);
			tile = game.getTileAt(to);
			
			if (unitFrom != null) 
			{
				if (unitTo != null)
				{
					if (unitFrom.getOwner() != unitTo.getOwner())
						editor.showStatus(unitFrom.getOwner() + " " + unitFrom.getTypeString() + " is attacking " + unitTo.getOwner() + " " + unitTo.getTypeString());
				}
				else
				{
					if (tile.getTypeString() == GameConstants.MOUNTAINS)
							editor.showStatus("This unit cannot climb mountains");
					else if (tile.getTypeString() == GameConstants.OCEANS) 
						editor.showStatus("This unit cannot swim");
					else
						editor.showStatus("Ready to move");
				}
				
			} 
		}
	}
	
	public void mouseUp(MouseEvent e, int x, int y) 
	{
		Position to = GfxConstants.getPositionFromXY(x, y);
	
		if (unitFrom != null) 
		{
			if(game.moveUnit(from, to))
				editor.showStatus("Move successful");
			else
				editor.showStatus("Unable to move there");
		}
	}
	
}
